import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import Login from '@/components/Login'
import Home from "@/components/Home"
import Welcome from "@/components/Home/Welcome"
import Users from "@/components/Home/Users"
import Rights from '@/components/Home/Rights'
import Roles from '@/components/Home/Roles'
import Cate from '@/components/Home/Cate'
import Params from '@/components/Home/Params'
import Goods from '@/components/Home/Goods'
import Order from '@/components/Home/Order'
import Reports from '@/components/Home/Reports'
import Add from '@/components/Home/Goods/Add'
const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: Login
  },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      {
        path: "/welcome",
        component: Welcome
      },
      {
        path: "/users",
        component: Users
      },
      {
        path: "/rights",
        component: Rights
      },
      {
        path: "/roles",
        component: Roles
      },
      {
        path: "/categories",
        component: Cate
      },
      {
        path: "/params",
        component: Params
      },
      {
        path: "/goods",
        component: Goods,
      },
      {
        path: "/goods/add",
        component: Add,
      },
      {
        path: "/orders",
        component: Order
      },
      {
        path: "/reports",
        component: Reports
      },
    ]
  },

]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  const tokenStr = sessionStorage.getItem("token");
  if (!tokenStr) return next("/login")
  next()
})

export default router
